package com.eedi.framework.oauth2.dal.mapper;

import cn.hutool.core.util.ObjUtil;
import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.oauth2.controller.org.vo.client.OrgOAuth2ClientPageReqVO;
import com.eedi.framework.oauth2.dal.dataobject.OrgOAuth2ClientDO;
import org.apache.ibatis.annotations.Mapper;


/**
 * OAuth2 客户端 Mapper
 *
 * @author 永聚长青源码
 */
@Mapper
public interface OrgOAuth2ClientMapper extends BaseMapperX<OrgOAuth2ClientDO> {

    default PageResult<OrgOAuth2ClientDO> selectPage(OrgOAuth2ClientPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrgOAuth2ClientDO>()
                .likeIfPresent(OrgOAuth2ClientDO::getName, reqVO.getName())
                .eqIfPresent(OrgOAuth2ClientDO::getStatus, reqVO.getStatus())
                .orderByDesc(OrgOAuth2ClientDO::getId));
    }

    default OrgOAuth2ClientDO selectByClientCode(String clientCode) {
        return selectOne(OrgOAuth2ClientDO::getClientCode, clientCode);
    }

    default String getSysClientIdByOrgInfoId(String orgInfoId){
        OrgOAuth2ClientDO orgOauth2Client = selectOne(OrgOAuth2ClientDO::getOrgInfoId, orgInfoId);
        if (ObjUtil.isNotNull(orgOauth2Client)){
            return orgOauth2Client.getClientCode();
        }
        return null;
    }

    default String getSysClientIdByOrgInfoIdAndUserType(String orgInfoId, UserTypeEnum userType){
        OrgOAuth2ClientDO orgOauth2Client = selectOne(OrgOAuth2ClientDO::getOrgInfoId, orgInfoId
        ,OrgOAuth2ClientDO::getOrgOauth2ClientUserType,userType);
        if (ObjUtil.isNotNull(orgOauth2Client)){
            return orgOauth2Client.getClientCode();
        }
        return null;
    }

}
